//
//  CourseDetailCommentView.m
//  TeacherNet
//
//  Created by slz on 2017/11/15.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "CourseDetailCommentView.h"
#import "CourseCommentUserTableViewCell.h"

@implementation CourseCommentCountView
- (instancetype)init
{
    self = [super init];
    if (self){
        self.width = kScreenWidth;
        UIView * lineView = [UIView new];
        [self addSubview:lineView];
        lineView.backgroundColor = kLineColor_Gray_Default;
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.and.bottom.mas_equalTo(self);
            make.height.mas_equalTo(1);
        }];
        
    }
    return self;
}

- (CGFloat)updateWithQuickComments:(NSArray *)quickComments
{
    
    for (id obj in self.subviews) {
        if ([obj isKindOfClass:[UILabel class]]){
            UILabel * label = obj;
            [label removeFromSuperview];
        }
    }
    if (!quickComments || quickComments.count == 0)
    {
        self.height = 0;
        self.hidden = YES;
        return 0;
    }else
    {
        CGFloat labelWidth = (kScreenWidth - 36) * .5;
        NSInteger lineHeight = 15;
        NSInteger lineSpace = 10;
        NSInteger count = quickComments.count;
        self.height = count%2 == 0 ? count/2 * lineHeight : (count/2 + 1) * lineHeight;
        for (int i = 0; i < count; i ++)
        {
            CourseCommentQuickCommentModel * model = quickComments[i];
            UILabel * label = [UILabel new];
            NSString * showString = [NSString stringWithFormat:@"%@(%@)",model.quickCommentContent,model.commentCount];
            label.text = showString;
            label.fontSize = 15;
            label.textColor = kDark_TextColor_Default;
            label.left = i%2 == 0? 18:18 + labelWidth;
            label.size = CGSizeMake(labelWidth, 15);
            label.top = ((int)i/2 * (lineHeight + lineSpace)) + 18;
            [self addSubview:label];
        }
        NSInteger lineCount = count %2 >0 ? count/2 + 1 : count/2;
        NSInteger allLineSpaceHeight = lineCount > 1? (lineCount - 1) *lineSpace : 0;
        return lineCount * lineHeight + allLineSpaceHeight + 36;
    }
}

@end

@implementation CourseCommentAverageCommentView
{
    UILabel * _commentCountLabel;
}
- (instancetype)init
{
    self = [super init];
    if (self){
        _titleLabel = [UILabel new];
        _titleLabel.fontSize = 15;
        _titleLabel.textColor = kDark_TextColor_Default;
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(18);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
        _ratingView = [EDStarRating new];
        _ratingView.backgroundColor = [UIColor whiteColor];
        _ratingView.starImage = [UIImage imageNamed:@"course_star_comment"];
        _ratingView.starHighlightedImage = [UIImage imageNamed:@"course_star_comment_highlighted"];
        _ratingView.maxRating=5.0;
        _ratingView.horizontalMargin = 10;
        _ratingView.editable=NO;
        _ratingView.displayMode=EDStarRatingDisplayAccurate;
        [self addSubview:_ratingView];
        [_ratingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(85);
            make.centerY.mas_equalTo(self.mas_centerY);
            make.width.mas_equalTo(150);
            make.height.mas_equalTo(self);
        }];
        
        _commentCountLabel = [UILabel new];
        _commentCountLabel.textAlignment =NSTextAlignmentRight;
        _commentCountLabel.fontSize = 15;
        [self addSubview:_commentCountLabel];
        [_commentCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-18);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
        
        
        UIView * lineView = [UIView new];
        [self addSubview:lineView];
        lineView.backgroundColor = kLineColor_Gray_Default;
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.and.bottom.mas_equalTo(self);
            make.height.mas_equalTo(1);
        }];
    }
    return self;
}

- (void)setWithStarFloat:(CGFloat)starFloat peopleCount:(NSInteger)count
{
    NSString * aaa = @"参与";
    NSString * countString = [NSString stringWithFormat:@"%ld",count];
    NSAttributedString * att = [[NSAttributedString alloc]initWithString:countString attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
    NSMutableAttributedString * firstString = [[NSMutableAttributedString alloc]initWithString:aaa];
    [firstString appendAttributedString:att];
    NSAttributedString * ren = [[NSAttributedString alloc]initWithString:@"人"];
    [firstString appendAttributedString:ren];
    
    _commentCountLabel.attributedText = firstString;
    _titleLabel.text = @"综合评价";
    _ratingView.rating = starFloat;
}

@end

@implementation CourseCommentTableHeaderView
- (instancetype)init
{
    self = [super init];
    if (self){
        self.size = CGSizeMake(kScreenWidth, 146);
        _averageCommentView = [CourseCommentAverageCommentView new];
        _averageCommentView.origin = CGPointMake(0, 0);
        _averageCommentView.size = CGSizeMake(kScreenWidth, 45);
        [self addSubview:_averageCommentView];
      
        
        _commentView = [CourseCommentCountView new];
        _commentView.origin = CGPointMake(0, _averageCommentView.bottom);
        [self addSubview:_commentView];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self){
        self.size = CGSizeMake(kScreenWidth, 146);
        _averageCommentView = [CourseCommentAverageCommentView new];
        _averageCommentView.origin = CGPointMake(0, 0);
        _averageCommentView.size = CGSizeMake(kScreenWidth, 45);
        [self addSubview:_averageCommentView];
        
        
        _commentView = [CourseCommentCountView new];
        _commentView.origin = CGPointMake(0, _averageCommentView.bottom);
        [self addSubview:_commentView];
    }
    return self;
}
@end

@implementation CourseDetailCommentView
{
    CourseCommentPageModel * _model;
}
-(instancetype)init{
    self = [super init];
    if (self){
        self.backgroundColor = [UIColor blueColor];
        
        
        _tableView = [[YWTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.bounces = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 100;
        [self addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
        _headerView = [CourseCommentTableHeaderView new];
        [_tableView setTableHeaderView:_headerView];
    }
    return self;
}

- (void)updateWithPageObj:(id)obj
{
    NSDictionary * dict = [[MFWJson jsonWithObject:obj] toDic];
    if (!dict || dict.count == 0) return;
    CourseCommentPageModel * pageModel = [CourseCommentPageModel yy_modelWithDictionary:dict];
    _model = pageModel;
    [self.headerView.averageCommentView setWithStarFloat:_model.commentAverage peopleCount:_model.commentCount];
    
    self.headerView.commentView.height =  [self.headerView.commentView updateWithQuickComments:_model.quickComment];
    self.headerView.height = _headerView.averageCommentView.height + _headerView.commentView.height;
    _tableView.tableHeaderView = _headerView;
    [_tableView reloadData];
    
}

#pragma mark - UITbaleViewDelegate


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CourseUserCommentModel * model = _model.userComments[indexPath.row];
    static NSString * reuserIdentifier = @"CourseCommentUserTableViewCell";
    CourseCommentUserTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuserIdentifier];
    if (!cell) {
        cell = [CourseCommentUserTableViewCell instanceWithNibName:@"CourseCommentUserTableViewCell" owner:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.timeLabel.text = model.commentDate;
    cell.UserNameLabel.text = model.userName;
    cell.commentContentLabel.text = model.commentContent;
    cell.ratingView.rating = model.rating;
    [cell.avatar sd_setImageWithURL:[NSURL URLWithString:kAPI_IMAGE_HOST(model.avater)] randomPlaceHolder:YES];
    return cell;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _model.userComments.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}



@end
